#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project ：python常用模块库 
@File ：logger.py
@IDE  ：PyCharm 
@Author ：李涵彬
@Date ：2025/2/21 上午8:58 
"""

import logging
from logging.handlers import RotatingFileHandler
from pathlib import Path
from typing import Dict


def setup_logging(config: Dict):
	"""配置日志系统"""
	log_path = Path(config["logging"]["path"])
	log_path.mkdir(parents=True, exist_ok=True)

	formatter = logging.Formatter(
		"%(asctime)s [%(levelname)s] %(name)s: %(message)s",
		datefmt="%Y-%m-%d %H:%M:%S"
	)

	file_handler = RotatingFileHandler(
		log_path / "file_monitor.log",
		maxBytes=10 * 1024 * 1024,  # 10MB
		backupCount=5,
		encoding="utf-8"
	)
	file_handler.setFormatter(formatter)

	console_handler = logging.StreamHandler()
	console_handler.setFormatter(formatter)

	logging.basicConfig(
		level=config["logging"].get("level", "INFO"),
		handlers=[file_handler, console_handler]
	)
